package com.idroid.overlays;

import java.util.ArrayList;

import org.osmdroid.ResourceProxy;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.OverlayItem;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.idroid.activities.ParkingDetailActivity;
import com.idroid.domain.HookableParkingArea;

public class UpdateItemizedIconOverlay extends
		ItemizedOverlayWithMultipleFocus<OverlayItem> {

	public UpdateItemizedIconOverlay(final MapView mapView,
			Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
		this(mapView, null, NOT_SET, pResourceProxy);

	}

	public UpdateItemizedIconOverlay(final MapView mapView,
			Drawable defaultMarker, int pFocusedBackgroundColor,
			ResourceProxy pResourceProxy) {
		super(new ArrayList<OverlayItem>(), pFocusedBackgroundColor, null,
				pResourceProxy);

		this.mOnItemGestureListener = new OnItemGestureListener<OverlayItem>() {
			public boolean onItemSingleTapUp(int index, OverlayItem item) {

				// Toast.makeText(
				// mapView.getContext(),
				// "Item  (" + item.mDescription
				// + ") got single tapped up", Toast.LENGTH_LONG)
				// .show();
				return false;
			}

			public boolean onItemLongPress(final int index,
					final OverlayItem item) {
				// Toast.makeText(mapView.getContext(),
				// "Item  (" + item.mDescription + ") got long pressed",
				// Toast.LENGTH_LONG).show();

				return false;
			}
		};

		this.setFocusItemsOnTap(true);
	}

	public synchronized void addDrawable(String id, String description,
			GeoPoint center, Drawable drawable) {

		OverlayItem ellipseItem = new OverlayItem(id, description, center);
		ellipseItem.setMarker(drawable);

		this.addItem(ellipseItem);

	}

	@Override
	public synchronized void draw(Canvas arg0, MapView arg1, boolean arg2) {
		synchronized (mItemList) {

			if (mFocusedItemIndex >= mItemList.size()) {
				System.out.println("Avoiding ArrayIndexOutOfBounds");
				mFocusedItemIndex = NOT_SET;
				return;
			} else {
				super.draw(arg0, arg1, arg2);
			}
		}

	}

	public synchronized void addDrawable(OverlayItem item, Drawable drawable) {

		item.setMarker(drawable);

		this.addItem(item);

	}

	public synchronized void removeDrawable(OverlayItem item) {

		this.removeItem(item);

	}

	@Override
	public synchronized int size() {

		return super.size();

	}

	public OnItemGestureListener<OverlayItem> getOnItemGestureListener() {
		return mOnItemGestureListener;

	}

	public void setOnItemGestureListener(
			OnItemGestureListener<OverlayItem> gestureListener) {
		this.mOnItemGestureListener = gestureListener;

	}

}
